package com.hj.security.ui;

import java.util.Vector;

import com.hj.security.R;
import com.hj.security.domain.CacheInfo;
import com.hj.security.engine.CacheInfoProvider;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CacheClearActivity extends ListActivity{

	protected static final int LOADING = 0;
	protected static final int FINISH = 1;
	private CacheInfoProvider provider;
	private ListView lv_cache_list;
	private LinearLayout ll_cache_clear_load;
	
	private Vector<CacheInfo> cacheInfos;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case LOADING:
				ll_cache_clear_load.setVisibility(View.VISIBLE);
				break;
			case FINISH:
				ll_cache_clear_load.setVisibility(View.INVISIBLE);
				cacheInfos = provider.getCacheInfos();
				lv_cache_list.setAdapter(new CacheAdapter());
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.cache_clear);
		
		provider = new CacheInfoProvider(handler, this);
		ll_cache_clear_load = (LinearLayout) findViewById(R.id.ll_cache_clear_load);
		lv_cache_list = (ListView) findViewById(android.R.id.list);
		
		lv_cache_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
				intent.addCategory(Intent.CATEGORY_DEFAULT);
				intent.setData(Uri.parse("package:"+cacheInfos.get(position).getPackageName()));
				startActivity(intent);
			}
		});
		loadData();
	}

	private void loadData() {
		ll_cache_clear_load.setVisibility(View.VISIBLE);
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				provider.initCacheInfos();
			}
		}).start();
	}
	private class CacheAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return cacheInfos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return cacheInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view;
			ViewHolder holder;
			CacheInfo cacheInfo = cacheInfos.get(position);
			if(convertView==null){
				view = View.inflate(CacheClearActivity.this, R.layout.cache_clear_item, null);
				holder = new ViewHolder();
				holder.iv_cache_icon = (ImageView) view.findViewById(R.id.iv_cache_icon);
				holder.tv_cache_name = (TextView) view.findViewById(R.id.tv_cache_name);
				holder.tv_code_size = (TextView) view.findViewById(R.id.tv_code_size);
				holder.tv_data_size = (TextView) view.findViewById(R.id.tv_data_size);
				view.setTag(holder);
			}else{
				view = convertView;
				holder = (ViewHolder) view.getTag();
			}
			holder.iv_cache_icon.setImageDrawable(cacheInfo.getIcon());
			holder.tv_cache_name.setText(cacheInfo.getName());
			holder.tv_cache_size.setText(cacheInfo.getCacheSize());
			holder.tv_code_size.setText(cacheInfo.getCodeSize());
			holder.tv_data_size.setText(cacheInfo.getCodeSize());
			return view;
		}
		
	}
	private class ViewHolder{
		ImageView iv_cache_icon;
		TextView tv_cache_name;
		TextView tv_code_size;
		TextView tv_cache_size;
		TextView tv_data_size;
	}
}
